local Musha_Leader =
	Class(
	function(self, inst)
		if not inst.components.musha_keyhandler then
			inst:AddComponent("musha_keyhandler")
		end
		inst.components.musha_keyhandler:AddActionListener(
			"musha",
			inst.musha_settings.Keys.ORDER_MUSHA_YAMCHE_1,
			"Order_Musha_Yamche_1"
		)
		inst.components.musha_keyhandler:AddActionListener(
			"musha",
			inst.musha_settings.Keys.ORDER_MUSHA_YAMCHE_2,
			"Order_Musha_Yamche_2"
		)
		inst.components.musha_keyhandler:AddActionListener(
			"musha",
			inst.musha_settings.Keys.ORDER_MUSHA_BEEFALO,
			"Order_Musha_Beefalo"
		)
		inst.components.musha_keyhandler:AddActionListener(
			"musha",
			inst.musha_settings.Keys.ORDER_MUSHA_MOONTREE,
			"Order_Musha_MoonTree"
		)

		if TheNet:GetIsServer() then
			self.inst = inst
			self.musha_followers = {}
		end
	end
)

if TheNet:GetIsClient() then
	return Musha_Leader
end

function Musha_Leader:AddFollower(name, follower)
	follower.ownerid = self.inst.userid
	self.musha_followers[name] = follower
	follower:ListenForEvent(
		"death",
		function(follower)
			if self.musha_followers[name] == follower then
				self.musha_followers[name] = nil
			end
		end
	)
end

function Musha_Leader:OnSave()
	local follower_data = {}
	for name, entity in pairs(self.musha_followers) do
		follower_data[name] = {}
		follower_data[name]["record"], follower_data[name]["references"] = entity:GetSaveRecord()
	end
	TheWorld.Musha_Watcher.playerdata[self.inst.userid].followers = follower_data
	return self.following
end

function Musha_Leader:OnLoad(data)
	self.inst:DoTaskInTime(
		0,
		function(inst)
			if TheWorld.Musha_Watcher.playerdata[inst.userid].followers then
				for name, data in pairs(TheWorld.Musha_Watcher.playerdata[inst.userid].followers) do
					local entity = SpawnSaveRecord(data["record"], data["references"])
					self:AddFollower(name, entity)
					if data[name] then
						local pos = inst:GetPosition()
						local offset = FindWalkableOffset(pos, math.random(1, 360) * DEGREES, math.random(1, 5), 30, false, false)
						entity.Transform:SetPosition(pos.x + offset.x, pos.y, pos.z + offset.z)
					end
				end
			end
		end
	)

	self.following = data
end

function Musha_Leader:Order_Musha_Beefalo(inst)
	if self.musha_followers["musha_beefalo"] then
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			if inst.components.leader:CountFollowers("musha_beefalo") > 0 then
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_STAY)
				self.musha_followers["musha_beefalo"]:order(inst, "stay")
			else
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_OOOOH)
			end
		else
			if inst.components.leader:CountFollowers("musha_beefalo") > 0 then
				self.musha_followers["musha_beefalo"]:order(inst, "stay")
			else
				self.musha_followers["musha_beefalo"]:order(inst, "follow")
			end
		end
	else
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			return
		end
		local x, y, z = inst.Transform:GetWorldPosition()
		local ents = TheSim:FindEntities(x, y, z, 25, {"musha_beefalo"})
		if next(ents) == nil then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_ARONG_LOST)
			return
		end
		for k, v in pairs(ents) do
			if v.owner == nil then
				self:AddFollower("musha_beefalo", v)
				self:Order_Musha_Beefalo(inst)
				return
			end
		end
	end
end

function Musha_Leader:Order_Musha_Yamche_1(inst) --follow
	musha_print("Order_Musha_Yamche_1")
	if self.musha_followers["musha_yamche"] then
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			if inst.components.leader:CountFollowers("musha_yamche") > 0 then
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_STAY)
				self.musha_followers["musha_yamche"]:order(inst, "stay")
			else
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_OOOOH)
			end
		else
			if inst.components.leader:CountFollowers("musha_yamche") > 0 then
				inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_STAY)
				self.musha_followers["musha_yamche"]:order(inst, "stay")
			else
				inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_FOLLOW)
				self.musha_followers["musha_yamche"]:order(inst, "follow")
			end
		end
	else
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			return
		end
		local x, y, z = inst.Transform:GetWorldPosition()
		local ents = TheSim:FindEntities(x, y, z, 25, {"musha_yamche"})
		if next(ents) == nil then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_ARONG_LOST)
			return
		end
		for k, v in pairs(ents) do
			if v.owner == nil then
				self:AddFollower("musha_yamche", v)
				self:Order_Musha_Yamche_1(inst)
				return
			end
		end
	end
end

function Musha_Leader:Order_Musha_Yamche_2(inst) --action
	musha_print("Order_Musha_Yamche_2")
	if inst.components.health:IsDead() or inst:HasTag("playerghost") then
		inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_GHOST)
	end
	if inst.components.leader:CountFollowers("musha_yamche") > 0 then
		if not self.musha_followers["musha_yamche"].components.container then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_YAMCHE_REVIVE_D)
			return
		end
		if self.musha_followers["musha_yamche"].action == "fight" then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_GATHER)
			self.musha_followers["musha_yamche"]:order(inst, "avoid")
		elseif self.musha_followers["musha_yamche"].action == "avoid" then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_STUFF)
			self.musha_followers["musha_yamche"]:order(inst, "pick")
		elseif self.musha_followers["musha_yamche"].action == "pick" then
			if self.musha_followers["musha_yamche"].type ~= "green" then
				inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_DEFFENSE)
				self.musha_followers["musha_yamche"]:order(inst, "fight")
			else
				inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_YAMCHE_GATHER)
				self.musha_followers["musha_yamche"]:order(inst, "avoid")
			end
		end
	end
end

function Musha_Leader:Order_Musha_MoonTree(inst)
	if self.musha_followers["musha_moontree"] then
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			if inst.components.leader:CountFollowers("musha_moontree") > 0 then
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_STAY)
				self.musha_followers["musha_moontree"]:order(inst, "stay")
			else
				inst.components.talker:Say(STRINGS.MUSHA_TALK_GHOST_OOOOH)
			end
		else
			if inst.components.leader:CountFollowers("musha_moontree") > 0 then
				self.musha_followers["musha_moontree"]:order(inst, "stay")
			else
				self.musha_followers["musha_moontree"]:order(inst, "follow")
			end
		end
	else
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			return
		end
		local x, y, z = inst.Transform:GetWorldPosition()
		local ents = TheSim:FindEntities(x, y, z, 25, {"musha_moontree"})
		if next(ents) == nil then
			inst.components.talker:Say(STRINGS.MUSHA_TALK_ORDER_DALL_LOST)
			return
		end
		for k, v in pairs(ents) do
			if v.owner == nil then
				self:AddFollower("musha_moontree", v)
				self:Order_Musha_MoonTree(inst)
				return
			end
		end
	end
end

return Musha_Leader
